﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using VietPublisher.BSL.BK;
using VietPublisher.Repositories.Interface;
using VietPublisher.BaseProject;
using VietPublisher.Web.Framework;
using VietPublisher.BaseProject.Models;
using VietPublisher.API.Membership;
using System.Text.RegularExpressions;

namespace VietPublisher.Web.BK.Controllers.Admin
{
    public class AdminController : StringBaseEntityController
    {


        /// <summary>
        /// service
        /// </summary>
        public IFormsAuthenticationService FormsService { get; set; }
        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            base.Initialize(requestContext);
        }


        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View("Index");
        }


        //[ActionName("AdminHomePage")]
        [CustomAuthorize(Roles = SiteRoles.Admin)]
        public ActionResult TestMT()
        {
            return View("TestMT");
        }


        public ActionResult Login()
        {

            return View();
        }



        /// <summary>
        /// action login for admin
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(FormCollection collection)
        {

            bool result = false;
            string userName = Request.Form["userName"] != null ? Request.Form["userName"].ToString() : "";
            string passWord = Request.Form["passWord"] != null ? Request.Form["passWord"].ToString() : "";
            result = RepoAccount.Login(userName, passWord);
            if (result)
            {
                FormsService.SignIn(userName, true);
                StoreSessionInfo();
                #region auto redirect to the page when login success
                //string returnUrl = Request.Form["returnUrl"] != null ? Request.Form["returnUrl"].ToString() : "";
                string returnUrl = "";
                try
                {
                    returnUrl = Regex.Split(Request.UrlReferrer.PathAndQuery, "ReturnUrl=")[1];
                }
                catch
                {
                }

                returnUrl = System.Web.HttpUtility.UrlDecode(returnUrl);
                if (!String.IsNullOrEmpty(returnUrl))
                {

                    return Content(returnUrl);
                }
                else
                {
                    return Content("/Book/BookList");
                    //return RedirectToAction("Login", "Admin");
                }
                #endregion
            }
            else
            {
                FormsService.SignOut();
                return Content(result.ToString());
            }

        }//close login

        [Authorize]
        public ActionResult LogOff()
        {
            FormsService.SignOut();
            Session.RemoveAll();
            Session.Clear();
            Session.Abandon();

            return RedirectToAction("Login", "Admin");
        }

    }
}
